import {iot} from "../node-test";
import log from './logger'
import {AliyunIot} from "./interfaces";
import settings from "./settings";
import player from "./player";
import controller from "./controller";
import weighter from "./weight";
import plate from "./plate";
import state from "./states/state";
import printer from "./printer";

class AliyunIotImpl implements AliyunIot {
    device: any

    onWeightData(weight: number) {
        this.device.postProps({weight});
        if (weight < 0.1) {
            this.device.postProps({vehicleNumber: "无车牌"});
        }
    }

    onWeightDisconnect() {
        this.device.postEvent('weight_disconnect', {});
    }

    onWen(wen: boolean) {
        // this.device.postProps({ wen: wen ? 0 : 1 });
    }

    onActive(DI: boolean[]) {
        this.device.postProps({
            di: [DI[0] ? 0 : 1, DI[1] ? 0 : 1, DI[2] ? 0 : 1, DI[3] ? 0 : 1],
        }, (res: any) => {
            // log.info(res);
        });
    }

    onActiveD(RL: boolean[]) {
        this.device.postProps({
            rl: [RL[0] ? 0 : 1, RL[1] ? 0 : 1, RL[2] ? 0 : 1, RL[3] ? 0 : 1],
        }, (res: any) => {
            // log.info(res);
        });
    }

    onPlate(vehicleNumber: string, inout: string) {
        this.device.postEvent('plate', {vehicleNumber, inout});
        this.device.postProps({vehicleNumber});
    }

    sayService(res: any, reply: any) {
        log.info('say async called,res:' + JSON.stringify(res));
        player.say(res.params.text);
        const result = {
            data: {},
            code: 200
        }
        reply(result);
    }

    openRLService(res: any, reply: any) {
        log.info('up async called,res:' + JSON.stringify(res));
        controller.onlyOpenRL(res.params.i);
        const result = {
            data: {},
            code: 200
        }
        reply(result);
    }

    closeRLService(res: any, reply: any) {
        log.info('down async called,res:' + JSON.stringify(res));
        controller.onlyCloseRL(res.params.i);
        const result = {
            data: {},
            code: 200
        }
        reply(result);
    }

    snapService(res: any, reply: any) {
        log.info('snap async called,res:' + JSON.stringify(res));
        // const fileNames = vm.$store.state.video.saveImg() // todo
        // const fileNames = [{name: "abcd"}]
        // const fileNamesArray = []
        // for (let index = 0; index < fileNames.length; index++) {
        //     fileNamesArray.push(fileNames[index].name);
        // }
        const result = {
            data: {
                // photoArrays: fileNamesArray,
            },
            code: 200
        }
        log.info('result' + JSON.stringify(result));
        reply(result, 'sync');
    }

    printService(res: any, reply: any) {
        log.info('print async called,res:' + JSON.stringify(res));
        printer.print(res.params.count, res.params.data, res.params.type, res.params.paper);
        const result = {
            data: {},
            code: 200
        }
        reply(result);
    }

    driverRequestService(req: any, reply: any) {
        log.info('driverRequest async called,res:' + JSON.stringify(req));
        state.driverRequest(req.params.type);
        const result = {
            data: {},
            code: 200
        };
        reply(result);
    }

    onProps(cmd: any) {
        log.info('>>>onProps', cmd); //打印完整的属性设置消息
        for (let key in cmd.params) {
            if (key === 'enable') { //判断是否设置的是LightSwitch属性
                log.info('set property ', key);
                //示例代码将云端设置的属性在本地进行保存，实际产品开发时需要修改为去将灯打开或者关闭
                state.enable = cmd.params.enable === 1;
                //本地设置完毕之后，将更新后的状态报告给云端。
                //注意：云端下发命令后，云端属性的值并不会改变，云端需要等待来自设备端的属性上报
                this.device.postProps({'enable': cmd.params.enable});
            } else if (key === 'ignore') {
                log.info('set property ', key);
                //示例代码将云端设置的属性在本地进行保存，实际产品开发时需要修改为去将灯打开或者关闭
                controller.ignore = cmd.params.enable === 1;
                //本地设置完毕之后，将更新后的状态报告给云端。
                //注意：云端下发命令后，云端属性的值并不会改变，云端需要等待来自设备端的属性上报
                this.device.postProps({'ignore': cmd.params.enable});
            }
        }
    }

    init() {
        this.device = iot.device({
            productKey: settings.productKey,
            deviceName: settings.deviceName,
            deviceSecret: settings.deviceSecret
        });
        this.device.on('connect', () => {
            log.info('aliIot connect successfully!');
        });

        weighter.on('weight_data', this.onWeightData.bind(this));
        weighter.on('weight_disconnect', this.onWeightDisconnect.bind(this))
        weighter.on('wen', this.onWen.bind(this));

        controller.on('active', this.onActive.bind(this));
        controller.on('activeD', this.onActiveD.bind(this));
        plate.on('plate', this.onPlate.bind(this))

        this.device.onService('say', this.sayService.bind(this));
        this.device.onService('openRL', this.openRLService.bind(this));
        this.device.onService('closeRL', this.closeRLService.bind(this));
        this.device.onService('snap', this.snapService.bind(this));
        this.device.onService('print', this.printService.bind(this));
        this.device.onService('driverRequest', this.driverRequestService.bind(this));
        // 监听云端设置属性服务消息
        this.device.onProps(this.onProps.bind(this));
    }
}

// const aliyunIot = new AliyunIotImpl();
// export default aliyunIot;